﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Top.Web;
using Top.Web.Api;
using Top.Web.Api.Filters;
using Top.Core;
using Top.PointStore.Business;
using Top.PointStore.Web.Filters;
//using Top.PointStore.Filters;

namespace Top.PointStore.Controllers
{
    /// <summary>
    /// 商品分类
    /// </summary>
    //[Login(false)]
    [Provider]
    public class ProductsCategoryController : ApiBase
    {
        /// <summary>获取商品分类数据列表
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> GetList()
        {
            return await ProductsCategoryManager.GetList(-1);
        }


        /// <summary>保存商品分类信息
        /// </summary>
        [Login]
        public async Task<object> Save(string categoryName, int kid, int parentKid)
        {
            var model = new T_ProductsCategory();
            model.Kid = kid;
            model.F_CategoryName = categoryName;
            model.F_ParentKid = parentKid;
            if (model.F_ParentKid > 0)
            {
                var obj = await ProductsCategoryManager.Get(model.F_ParentKid);
                model.F_ParentPath = obj.F_ParentPath + "," + model.F_ParentKid;
                model.F_Lever = obj.F_Lever + 1;
            }
            else
            {
                model.F_ParentPath = "0";
                model.F_Lever = 1;
            }
            return model.Kid > 0 ? await ProductsCategoryManager.Update(model) : await ProductsCategoryManager.Add(model);
        }

        /// <summary>根据编号获取一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        [Login]
        public async Task<object> Get(int kid)
        {
            return await ProductsCategoryManager.Get(kid);
        }

        /// <summary>删除记录
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        [Login]
        public async Task<object> Delete(string kids)
        {
            if (kids.Split(',').Length >= 2)
            {
                return await ProductsCategoryManager.DeleteList(kids);
            }
            return await ProductsCategoryManager.Delete(int.Parse(kids));
        }




        /// <summary>根据条件获取商品分类列表
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> GetListByPid(int parentKid = 0)
        {
            return await ProductsCategoryManager.GetList(parentKid);
        }

    }
}
